<?php
/**
 * 图片操作操作
 * @author guangting
 *
 */

class ImageHelper
{
	private static $_instance = null;
	private $src_file = '';
	
	public function __construct($src_file)
	{
		$this->src_file = $src_file;
	}
	
	public static function getInstance($src_file = '')
	{
		if (!get_extension_funcs('gd'))
		{
			die('"GD" extension Not Found');
		}
		
		if (self::$_instance)
		{
			return self::$_instance;
		}
		else
		{
			return new self($src_file);
		}
	}
	
	/**
	 * 等比缩放图片
	 * @param String $out_file 输出图片
	 * @param Array $config 
	 * @param String $depend 依赖属性，width|height，根据$depend自动计算要缩放的大小
	 */
	public function resize($out_file, $config, $depend = false)
	{
		if (is_array($depend))
		{
			$depend = current($depend);
		}
		$image_info = $this->get_image_info($this->src_file);
		$methods = $this->get_methods($image_info['type']);
		//获取图像资源
		$src_image = call_user_func_array($methods[0], array($this->src_file));
		if ($depend !== false) {
		  	$dst_size = $this->get_resize_size($image_info, $config, $depend);
		} else {
		    $dst_size = $config;
		}
		//创建画布
		$dst_image = imagecreatetruecolor($dst_size['width'], $dst_size['height']);

		if ($image_info['type'] == IMAGETYPE_PNG)
		{
			//处理png图片的透明背景
			imagesavealpha($src_image, true);
			//设置不合并颜色,直接用$src_image图像颜色替换,包括透明色;
			imagealphablending($dst_image, false);
			imagesavealpha($dst_image, true);
		}
		imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_size['width'], $dst_size['height'], $image_info['width'], $image_info['height']);
		call_user_func_array($methods[1], array($dst_image, $out_file));
		imagedestroy($dst_image);
		imagedestroy($src_image);
	}
	
	/**
	 * 自由剪切图片
	 * @param String $out_file
	 * @param Array $config
	 * @param Array $depend
	 */
	public function crop($out_file, $config, $depend = '')
	{
		$image_info = $this->get_image_info($this->src_file);
		$methods = $this->get_methods($image_info['type']);
		//获取图像资源
		$src_image = call_user_func_array($methods[0], array($this->src_file));
		$dst_size = array_merge(array(
			'x' => 0,
			'y' => 0,
			'width' => 0,
			'height' => 0
		), $config);
		
		if ($depend != '')
		{
			$dst_size = $this->get_crop_size($image_info, $dst_size, $depend);
		}
		
		//创建画布
		$dst_image = imagecreatetruecolor($dst_size['width'], $dst_size['height']);
		if ($image_info['type'] == IMAGETYPE_PNG)
		{
			//处理png图片的透明背景
			imagesavealpha($src_image, true);
			//设置不合并颜色,直接用$src_image图像颜色替换,包括透明色;
			imagealphablending($dst_image, false);
			imagesavealpha($dst_image, true);
		}
		imagecopy($dst_image, $src_image, 0, 0, $dst_size['x'], $dst_size['y'], $dst_size['width'], $dst_size['height']);
		call_user_func_array($methods[1], array($dst_image, $out_file));
		imagedestroy($dst_image);
		imagedestroy($src_image);
	}
	
	/**
	 * 等比缩放后自由剪切图片
	 * @param String $out_file
	 * @param Array $config
	 * @param Array $depend
	 */
	public function resize_to_crop($out_file, $config, $depend = false)
	{
		$image_info = $this->get_image_info($this->src_file);
		$methods = $this->get_methods($image_info['type']);
		//获取图像资源
		$src_image = call_user_func_array($methods[0], array($this->src_file));
		if (!empty($depend['resize'])) {
			$dst_size = $this->get_resize_size($image_info, $config, $depend['resize']);
		} else {
			$dst_size = $config;
		}
		//创建画布
		$dst_image = imagecreatetruecolor($dst_size['width'], $dst_size['height']);
	
		if ($image_info['type'] == IMAGETYPE_PNG)
		{
			//处理png图片的透明背景
			imagesavealpha($src_image, true);
			//设置不合并颜色,直接用$src_image图像颜色替换,包括透明色;
			imagealphablending($dst_image, false);
			imagesavealpha($dst_image, true);
		}
		imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_size['width'], $dst_size['height'], $image_info['width'], $image_info['height']);
		imagedestroy($src_image);
	
		$image_info['width'] = $dst_size['width'];
		$image_info['height'] = $dst_size['height'];
	
		$dst_size = array_merge(array(
			'x' => 0,
			'y' => 0,
			'width' => 0,
			'height' => 0
		), $config);
	
		if (!empty($depend['crop']))
		{
			$dst_size = $this->get_crop_size($image_info, $dst_size, $depend['crop']);
		}
	
		//创建画布
		$tmp_image = imagecreatetruecolor($dst_size['width'], $dst_size['height']);
		if ($image_info['type'] == IMAGETYPE_PNG)
		{
			//处理png图片的透明背景
			imagesavealpha($dst_image, true);
			//设置不合并颜色,直接用$src_image图像颜色替换,包括透明色;
			imagealphablending($tmp_image, false);
			imagesavealpha($tmp_image, true);
		}
		imagecopy($tmp_image, $dst_image, 0, 0, $dst_size['x'], $dst_size['y'], $dst_size['width'], $dst_size['height']);
		call_user_func_array($methods[1], array($tmp_image, $out_file));
		imagedestroy($dst_image);
		imagedestroy($tmp_image);
	}
	
	public function get_crop_size($image_info, $dst_size, $depend)
	{
		$size = array();
		switch ($depend[0])
		{
			case 'left':
				$size['x'] = 0;
				break;
			case 'center':
				$size['x'] = ($image_info['width'] - $dst_size['width']) / 2;
				break;
			case 'right':
				$size['x'] = $image_info['width'] - $dst_size['width'];
				break;
		}
		
		if (!isset($depend[1]))
		{
			$depend[1] = $depend[0];
		}
		
		switch ($depend[1])
		{
			case 'left':
				$size['y'] = 0;
				break;
			case 'center':
				$size['y'] = ($image_info['height'] - $dst_size['height']) / 2;
				break;
			case 'right':
				$size['y'] = 0;
				break;
		}
		return array_merge($dst_size, $size);
	}
	
	/**
	 * 获取按比例缩放的大小
	 */
	public function get_resize_size($image_info, $config, $depend)
	{
		$arr = array('width', 'height');
		$size = array();
		$indexs = array();
		foreach ($arr as $i => $item)
		{
			if ($item == $depend)
			{
				$size[$item] = $config[$depend];
				if (strpos($size[$item], '%') !== false)
				{
					$size[$item] = (intval($size[$item]) / 100) * $image_info[$item];
				}
				$indexs[0] = $item;
			}
			else 
			{
				$indexs[1] = $item;
			}
		}
		$size[$indexs[1]] = ($size[$indexs[0]] / $image_info[$indexs[0]]) * $image_info[$indexs[1]];
		return $size;
	}
	
	/**
	 * 添加水印
	 * @param string $src_file 源文件
	 * @param string $out_file 输出文件
	 * @param string $watermark_file 水印文件
	 */
	public function watermark($src_file, $out_file, $watermark_file)
	{
		
	}
	
	private function get_methods($type)
	{
		$methods = array();
		switch ($type)
		{
			case IMAGETYPE_GIF:
				$methods = array('imagecreatefromgif', 'imagegif');
				break;
			case IMAGETYPE_BMP:
				$methods = array('imagecreatefromjpeg', 'imagewbmp');
				break;
			case IMAGETYPE_PNG:
				$methods = array('imagecreatefrompng', 'imagepng');
				break;
			case IMAGETYPE_JPEG:
			default:
				$methods = array('imagecreatefromjpeg', 'imagejpeg');
		}
		return $methods;
	}
	
	public function get_image_info($file)
	{
		$image = getimagesize($file);
		return array(
			'width' => $image[0],
			'height' => $image[1],
			'type' => $image[2],
			'mime' => $image['mime']
		);
	}
}